class_name Weapon
extends Node2D

@export var weapon_data:WeaponData
@export var sprite2d:Sprite2D
@export var animation_player:AnimationPlayer
var blackboard_data:BlackboardData
var weapon_components:Array[WeaponC] = []
var weapon_component_dict:Dictionary = {}
var enable:bool = false
var owner_entity
var is_player:bool:
	get:
		return owner_entity and owner_entity == GamePlayerSystem.get_player()
signal euqip_weapon(weapon:Weapon)
signal unequip_weapon(weapon:Weapon)
signal discard_weapon(weapon:Weapon)

func init_all_weapon_component():
	for child in get_components():
		if child is WeaponC:
			weapon_components.append(child)
			weapon_component_dict[child.weapon_component_name] = child
			child.init_component(self)
func get_entity_data() -> WeaponData:
	return weapon_data
func get_blackboard_data(key:String):
	return blackboard_data.get_data(key)
func set_blackboard_data(key:String,value):
	blackboard_data.set_data(key,value)
func get_blackboard_data_dict() -> Dictionary:
	return blackboard_data.get_data_dict()
func clear_blackboard_data_dict():
	blackboard_data.clear_data_dict()
func get_component_dict()->Dictionary:
	return weapon_component_dict
func get_component_by_name(weapon_component_name:String) -> WeaponC:
	if weapon_component_dict.has(weapon_component_name):
		return weapon_component_dict[weapon_component_name]
	return null
func get_weapon_id() -> String:
	return weapon_data.weapon_id
func get_weapon_name() -> String:
	return weapon_data.weapon_name
func get_weapon_desc() -> String:
	return weapon_data.weapon_desc
func get_weapon_sprite() -> Texture2D:
	return weapon_data.weapon_sprite
func get_components() -> Array:
	var components:Array = []
	for child in get_children():
		if child is WeaponC:
			components.append(child)
	components.sort_custom(func (a,b):return a.priority < b.priority)
	return components
func equip(entity):
	owner_entity = entity
	enable = true
	var controller:ControllerComponent = owner_entity.get_component_by_name("ControllerComponent")
	if not controller.on_key_action.is_connected(on_key_action):
		controller.on_key_action.connect(on_key_action)
	for wc in weapon_components:
		wc.equip()
	euqip_weapon.emit(self)
	visible = true
func unequip():
	enable = false
	var controller:ControllerComponent = owner_entity.get_component_by_name("ControllerComponent")
	if controller.on_key_action.is_connected(on_key_action):
		controller.on_key_action.disconnect(on_key_action)
	for wc in weapon_components:
		wc.unequip()
	unequip_weapon.emit(self)
	visible = false
func discard():
	var controller:ControllerComponent = owner_entity.get_component_by_name("ControllerComponent")
	if controller.on_key_action.is_connected(on_key_action):
		controller.on_key_action.disconnect(on_key_action)
	var pickable_c = get_component_by_name("WeaponC_Pickable") as WeaponC_Pickable
	var slot_component = owner_entity.get_component_by_name("SlotComponent") as SlotComponent
	var weapon_pos = global_position
	var weapon_rot = global_rotation
	var root_pos = slot_component.get_slot_by_type(GameEnum.SlotType.Root).global_position
	pickable_c.init_drop_info(root_pos)
	unequip()
	owner_entity = null
	get_parent().remove_child(self)
	GameEffectSystem.add_child(self)
	global_position = weapon_pos
	global_rotation = weapon_rot
	for wc in weapon_components:
		wc.discard()
	discard_weapon.emit(self)
	visible = true
func on_key_action(key_action:GameEnum.KeyAction,key_Action_state:GameEnum.KeyActionState):
	var state_manager:WeaponC_StateManager = weapon_component_dict["WeaponC_StateManager"]
	if key_action == GameEnum.KeyAction.Attack and GameManager.get_cur_game_state() == GameEnum.GameState.Gaming:
		state_manager.set_fsm_expression_property(state_manager.expression_property_name_dict.attack,key_Action_state)

func _init() -> void:
	blackboard_data = BlackboardData.new()
	weapon_component_dict = {}
func _ready() -> void:
	for component in weapon_components:
		component.enable = true
	pass
func _process(delta: float) -> void:
	if not enable:
		return
